<?php

namespace App\Http\Resources\user;

use App\Models\config\Config;
use App\Models\user\UserSuperior;
use Illuminate\Http\Resources\Json\ResourceCollection;

class SuperiorPageCollection extends ResourceCollection
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    protected $count = 0;

    protected $all_count = 0;

    protected $sum = 0;

    public function __construct($resource,$count = 0,$all_count,$sum)
    {
        parent::__construct($resource);

        $this->resource = $this->collectResource($resource);
        $this->count = $count;
        $this->all_count = $all_count;
        $this->sum = $sum;
    }

    public function toArray($request)
    {
        return [
            'total' => $this->resource->total(),
            'perPage' => $this->resource->perPage(),
            'current_page' => $this->resource->currentPage(),
            'count' => $this->count,
            'all_count' => $this->all_count,
            'sum' => $this->sum,
            'items' => $this->collection->map(function (UserSuperior $item) {
                return [
                    'id' => $item->user_id,
                    'name' => $item->user->name ?? config('app.name')."用户",
                    'header_img' => $item->user->header_img ?? Config::getConfig('default_header'),
                    'phone' => $item->user->phone??'',
                    'level' => $item->user->level??0,
                    'sex' => $item->user->sex??1,
                    'created_time' => $item->user ? $item->user->created_at->toDateTimeString() : '',
                    'rebate' => $item->rebate??0.00
                ];
            })
        ];
    }
}
